package org.geektimes.projects.user.web.listener;

import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.spi.ConfigBuilder;
import org.eclipse.microprofile.config.spi.ConfigProviderResolver;
import org.geektimes.configuration.microprofile.config.converter.StringConverter;
import org.geektimes.configuration.microprofile.config.converter.StringToIntegerConverter;
import org.geektimes.configuration.microprofile.config.converter.StringToLongConverter;
import org.geektimes.configuration.microprofile.config.source.servlet.ServletContextConfigSource;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class ConfigInitializerListener implements ServletContextListener {

    public final static String microConfig = "microConfig";

    /**
     * Receives notification that the web application initialization
     * process is starting.
     *
     * <p>All ServletContextListeners are notified of context
     * initialization before any filters or servlets in the web
     * application are initialized.
     *
     * @param sce the ServletContextEvent containing the ServletContext
     *            that is being initialized
     */
   @Override
   public void contextInitialized(ServletContextEvent sce) {
       //org.eclipse.microprofile.config.spi.ConfigProviderResolver#loadSpi(ClassLoader cl)
       // ServiceLoader.load(ConfigProviderResolver.class, cl);
       //final Config config = ConfigProvider.getConfig();

       final ServletContext servletContext = sce.getServletContext();
       final ClassLoader classLoader = servletContext.getClassLoader();

       ConfigProviderResolver configProviderResolver = ConfigProviderResolver.instance();
       final ConfigBuilder builder = configProviderResolver.getBuilder();
       builder.forClassLoader(classLoader);
       builder.addDefaultSources();
       builder.addDiscoveredSources();
       builder.withConverters(
               new StringConverter()
               , new StringToIntegerConverter()
               , new StringToLongConverter()
       );
       ServletContextConfigSource servletContextConfigSource = new ServletContextConfigSource(servletContext);
       builder.withSources(servletContextConfigSource);

       final Config config = builder.build();

       final String javaVersion = config.getValue("java.version", String.class);
       final String applicationName = config.getValue("application.name", String.class);
       final Integer level = config.getValue("level", Integer.class);
       final String hello = config.getValue("hello", String.class);
       System.out.println("javaVersion:[" + javaVersion + "]");
       System.out.println("applicationName:[" + applicationName + "]");
       System.out.println("level:[" + level + "]");
       System.out.println("hello:[" + hello + "]");
       servletContext.setAttribute(microConfig,config);
   }

    /**
     * Receives notification that the ServletContext is about to be
     * shut down.
     *
     * <p>All servlets and filters will have been destroyed before any
     * ServletContextListeners are notified of context
     * destruction.
     *
     * @param sce the ServletContextEvent containing the ServletContext
     *            that is being destroyed
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
